//
//  CYListView.m
//  Demo
//
//  Created by lc4y on 2017/11/5.
//  Copyright © 2017年 lc4y. All rights reserved.
//

#import "CYListView.h"

@interface CYListView () 

/** 列表样式 **/
@property (nonatomic, assign) UITableViewStyle cy_style;

@end

@implementation CYListView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.cy_style = UITableViewStylePlain;
        self.cy_delegate = self;
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewStyle)style {
    self = [super init];
    if (self) {
        self.cy_style = style;
        self.cy_delegate = self;
    }
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    [self.cy_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];

}

#pragma mark - getter  setter
- (void)setCy_listScroll:(BOOL)cy_listScroll {
    self.cy_tableView.scrollEnabled = cy_listScroll;
}

- (void)setCy_dataArray:(NSArray *)cy_dataArray {
    _cy_dataArray = [cy_dataArray copy];
    
    if( cy_dataArray.count ) {
        // 数据源发生变化 刷新数据
        [self cy_hiddenNoDataView];
    } else {
        [self cy_showNoDataView];
    }
    
    [self.cy_tableView reloadData];
}

- (void)cy_hiddenNoDataView {
    if( self.cy_noDataView.superview ){
        [self.cy_noDataView removeFromSuperview];
    }
}

- (void)cy_showNoDataView {
    __weak typeof(self) weakSelf = self;
    self.cy_noDataView.cyAddToSuperView(self.cy_tableView);
    [self.cy_noDataView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
        make.width.mas_equalTo(weakSelf.cy_tableView.mas_width);
        make.height.mas_equalTo(weakSelf.cy_tableView.mas_height);
    }];
    
    [self.cy_noDataView cy_configNoDataImageN:self.cy_noDataImageN];
    [self.cy_noDataView cy_configNoDataTitleN:self.cy_noDataTitleN];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.cy_eventDelegate && [self.cy_eventDelegate respondsToSelector:@selector(cy_listViewDidScroll:)]) {
        [self.cy_eventDelegate cy_listViewDidScroll:scrollView];
    }
}

#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cy_dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(self.cy_delegate && [self.cy_delegate respondsToSelector:@selector(cy_cellIDWithIndexPath:)]) {
        if([self.cy_delegate cy_cellIDWithIndexPath:indexPath]) {
            self.cy_cellID = [self.cy_delegate cy_cellIDWithIndexPath:indexPath];
        }
    }
    
    UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:self.cy_cellID];
    tableViewCell.backgroundColor = [UIColor colorWithWhite:1.0f alpha:0];
    [self cy_refreshCell:tableViewCell indexPath:indexPath];
    return tableViewCell;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray <NSString *>*editTitleArray = nil;
    NSMutableArray <UITableViewRowAction *>*actionArray = [NSMutableArray new];
    if(self.cy_delegate && [self.cy_delegate respondsToSelector:@selector(cy_cellEditIndexPath:)]){
        editTitleArray = [self.cy_delegate cy_cellEditIndexPath:indexPath];
    }
    
    NSInteger index = 0;
    for (NSString *editTitle in editTitleArray) {
        UITableViewRowActionStyle actionStyle = [editTitle isEqualToString:@"删除"] ? UITableViewRowActionStyleDestructive : UITableViewRowActionStyleNormal;
        
        UITableViewRowAction *rowAction = [UITableViewRowAction rowActionWithStyle:actionStyle title:editTitle  handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            
            if(self.cy_delegate && [self.cy_delegate respondsToSelector:@selector(cy_cellEditIndexPath:)]){
                
                [self.cy_delegate cy_cellEditButtonClickAtIndex:action.index indexPath:indexPath];
            }
        }];
        
        rowAction.index = index++;
        [actionArray addObject:rowAction];
    }
    
    return !actionArray.count ? nil : actionArray;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if( self.cy_headerID ){
        return 44.0f;
    }
    
    return 0.000001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if(self.cy_delegate && [self.cy_delegate respondsToSelector:@selector(cy_sectionHeaderIDWithSection:)]) {
        if([self.cy_delegate cy_sectionHeaderIDWithSection:section]) {
            self.cy_headerID = [self.cy_delegate cy_sectionHeaderIDWithSection:section];
        }
    }
    UIView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.cy_headerID];
    [self cy_refreshHeader:headerView section:section];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if( self.cy_footerID ){
        return 44.0f;
    }
    
    return 0.000001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if(self.cy_delegate && [self.cy_delegate respondsToSelector:@selector(cy_sectionFooterIDWithSection:)]) {
        if([self.cy_delegate cy_sectionFooterIDWithSection:section]) {
            self.cy_footerID = [self.cy_delegate cy_sectionFooterIDWithSection:section];
        }
    }
    UIView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.cy_footerID];
    [self cy_refreshFooter:footerView section:section];
    return footerView;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.cy_listEdit;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.cy_tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(self.cy_eventDelegate && [self.cy_eventDelegate respondsToSelector:@selector(cy_didSelectCellRow:)]) {
       [self.cy_eventDelegate cy_didSelectCellRow:indexPath];
    }
}

-(void)cy_cellEditButtonClickAtIndex:(NSInteger)index indexPath:(NSIndexPath *)indexPath {
    if(self.cy_eventDelegate && [self.cy_eventDelegate respondsToSelector:@selector(cy_editCellRow:menuIndex:)]) {
        [self.cy_eventDelegate cy_editCellRow:indexPath menuIndex:index];
    }
}

- (kCYCustomCellID *)cy_cellIDWithIndexPath:(NSIndexPath *)indexPath { return nil; }

- (void)cy_refreshCell:(id)cell indexPath:(NSIndexPath *)indexPath {}

- (void)cy_refreshHeader:(id)headerView section:(NSInteger)section {}

- (void)cy_refreshFooter:(id)footerView section:(NSInteger)section {}

#pragma mark - 功能方法
- (void)cy_registerCellClass:(NSString *)cellClassString {
    self.cy_cellID = cellClassString;
    [self.cy_tableView registerClass:NSClassFromString(cellClassString) forCellReuseIdentifier:cellClassString];
}

- (void)cy_registerCellNib:(NSString *)cellNibName {
    self.cy_cellID = cellNibName;
    [self.cy_tableView registerNib:[UINib nibWithNibName:cellNibName bundle:nil] forCellReuseIdentifier:cellNibName];
}

/** 注册 Header Class String  默认类名字符串作为HeaderID**/
- (void)cy_registerHeaderClass:(NSString *)headerClassString {
    self.cy_headerID = headerClassString;
    [self.cy_tableView registerClass:NSClassFromString(headerClassString) forHeaderFooterViewReuseIdentifier:headerClassString];
}

/** 注册 Header Nib Name 默认NibName作为HeaderID**/
- (void)cy_registerHeaderNib:(NSString *)headerNibName {
    self.cy_headerID = headerNibName;
    [self.cy_tableView registerNib:[UINib nibWithNibName:headerNibName bundle:nil] forHeaderFooterViewReuseIdentifier:headerNibName];
}

/** 注册 Footer Class String  默认类名字符串作为FooterID**/
- (void)cy_registerFooterClass:(NSString *)footerClassString {
    self.cy_footerID = footerClassString;
    [self.cy_tableView registerClass:NSClassFromString(footerClassString) forHeaderFooterViewReuseIdentifier:footerClassString];
}

/** 注册 Footer Nib Name 默认NibName作为FooterID**/
- (void)cy_registerFooterNib:(NSString *)footerNibName {
    self.cy_footerID = footerNibName;
    [self.cy_tableView registerNib:[UINib nibWithNibName:footerNibName bundle:nil] forHeaderFooterViewReuseIdentifier:footerNibName];
}

- (void)cy_addHeaderRefreshControl:(HeaderRefreshCallBack)refreshCallBack {
    self.cy_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        refreshCallBack();
    }];
    self.cy_tableView.mj_header.ignoredScrollViewContentInsetTop = kBaseTitleLabelHeight;
}

- (void)cy_addFooterRefreshMoreControl:(FooterRefreshMoreCallBack)refreshCallBack {
    self.cy_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        refreshCallBack();
    }];
}

- (void)cy_endRefreshControl {
    [self.cy_tableView.mj_header endRefreshing];
    [self.cy_tableView.mj_footer endRefreshing];
}

- (void)cy_listViewReloadView {
    [self.cy_tableView reloadData];
}

/** table的头视图 */
- (void)cy_setTableViewHeaderView:(UIView *)tableHeaderView {
    self.cy_tableView.tableHeaderView = tableHeaderView;
}

#pragma mark - lazy method
- (UITableView *)cy_tableView {
    if( !_cy_tableView ) {
        _cy_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:self.cy_style];
        _cy_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _cy_tableView.dataSource = self;
        _cy_tableView.delegate = self;
        _cy_tableView.backgroundColor = [UIColor clearColor];
        [self addSubview:_cy_tableView];
    }
    
    return _cy_tableView;
}

@end
